package com.weixing.mall.provider.controller.rpc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.model.dto.GoodPrintRpcDto;
import com.weixing.mall.provider.api.model.query.ActGoodsQuery;
import com.weixing.mall.provider.api.model.query.ErpGoodsQuery;
import com.weixing.mall.provider.api.model.vo.ActivityProductVo;
import com.weixing.mall.provider.api.model.vo.ErpProductVo;
import com.weixing.mall.provider.api.model.vo.ItemVo;
import com.weixing.mall.provider.model.domain.Goods;
import com.weixing.mall.provider.model.domain.Product;
import com.weixing.mall.provider.model.enums.GoodsFromEnum;
import com.weixing.mall.provider.model.query.ProductQuery;
import com.weixing.mall.provider.service.IGoodsPrintService;
import com.weixing.mall.provider.service.IGoodsService;
import com.weixing.mall.provider.service.IProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ProductRpcController
 * @Description
 * @Author Small
 * @Date 2019/10/23 19:00
 * @Version 1.0
 **/
@RestController
@RequestMapping("api/product")
public class ItemRpcController extends BaseController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsPrintService goodsPrintService;

    @GetMapping("findActProductVoByIds")
    public Result<List<ActivityProductVo>> findActProductVoByIds(long[] productIds) {
        List<ActivityProductVo> list = productService.findActProductVoByIds(productIds);
        return ResultUtil.success(list);
    }
    @GetMapping("findProductVo")
    public Result<List<ActivityProductVo>> findProductVo(@RequestBody ActGoodsQuery goodsQuery) {
        List<ActivityProductVo> list = productService.findActProductVo(goodsQuery);
        return ResultUtil.success(list);
    }
    @GetMapping("findByGoodsn")
    public Result<List<ActivityProductVo>> findByGoodsn( String goodSn) {
        ActGoodsQuery goodsQuery=new ActGoodsQuery();
        goodsQuery.setGoodsSn(goodSn);
        List<ActivityProductVo> list = productService.findActProductVo(goodsQuery);
        return ResultUtil.success(list);
    }
    @GetMapping("findProductVoByCreatedId")
    public Result<IPage<ActivityProductVo>> findProductVo(Page page, Long createdById) {
        IPage<ActivityProductVo> list = productService.findProductVoByCreatedId(page,createdById);
        return ResultUtil.success(list);
    }
    @GetMapping("findProductVoByProductSn")
    public Result<ItemVo> findProductVoByProductSn(String productSn) {
        ItemVo productVo = productService.findProductVoByProductSn(productSn);
        return ResultUtil.success(productVo);
    }
    @GetMapping("getBygoodSn")
    public List<ItemVo>  getBygoodSn(String goodSn) {
        List<Product> productVo = productService.list(new QueryWrapper<Product>().eq("goods_sn",goodSn));
        List<ItemVo> itemVos= Lists.newArrayList();
        for(Product product:productVo){
            ItemVo itemVo=new ItemVo();
            BeanUtils.copyProperties(product, itemVo);
            itemVo.setProductId(product.getId());
            itemVos.add(itemVo);
        }
        return itemVos ;
    }
    @GetMapping("findActProductVo")
    public Result findActProductVo(ActGoodsQuery goodsQuery) {
        IPage pageInfo = productService.findActProductVo(initMpPage(), goodsQuery);
        return ResultUtil.success(pageInfo);
    }

    @GetMapping("findById")
    public Result<ItemVo> findById(Long itemId) {
        Product product = productService.getById(itemId);
        Goods goods = goodsService.getById(product.getGoodsId());
        ItemVo itemVo = null;
        if (null != product) {
            itemVo = new ItemVo();
            BeanUtils.copyProperties(product, itemVo);
            itemVo.setProductId(product.getId());
            itemVo.setGoodsState(goods.getStatus());
            itemVo.setCatId(goods.getCatId());
            itemVo.setCatName(goods.getCatName());
            itemVo.setCostPrice(goods.getCostPrice());
        }
        return ResultUtil.success(itemVo);
    }

    @GetMapping(value = "selectProduct")
    public List<ItemVo> selectProduct(){
        return productService.selectProduct(new ProductQuery());
    }

    @PostMapping("getErpGoodsList")
    public Result<IPage<ErpProductVo>> getErpGoodsList(@RequestBody ErpGoodsQuery egq) {
        egq.setGoodsFrom(GoodsFromEnum.ERP.getValue());
        IPage<ErpProductVo>  pageInfo = productService.getErpGoodsList(egq.getPage(), egq);
        return ResultUtil.success(pageInfo);
    }
    @PostMapping("printGoods")
    public Result  printGoods(@RequestBody List<GoodPrintRpcDto> goodPrintRpcDto) {
        goodsPrintService.goodPrint(goodPrintRpcDto);
        return  ResultUtil.success();
    }
    public static void main(String[] args) {
        Product product = new Product();
        product.setGoodsSn("1111");

        ItemVo vo = new ItemVo();


        BeanUtils.copyProperties(product, vo);
    }

}
